<!-- 指令成本作业：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <Find :searchImg="false" :OriginalFilter="{ static: true }" :formList="formList2" @findClicked="findClicked2" />

    <div class="flex mt10" style="width: 100%; background-color: white">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <h1 class="f16">待完工指令明细</h1>
        <Table
          :tableHeader="tableHeader"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
          @selectList="selectTableList"
        />
        <PageModule
          :PageData="{ total, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange"
          @pageChange="handleCurrentChange"
        />
      </el-card>
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }">
        <div class="flex AIC">
          <h1 class="f16">已完工指令明细</h1>
          <el-input class="h-24 w-120 ml10" v-model="labour" placeholder="成型人工" clearable @change=""></el-input>
          <el-input class="h-24 w-120 ml10" v-model="charge" placeholder="管理费用" clearable @change=""></el-input>
          <el-button class="primaryBth ml10" type="primary" size="default" @click="addList">填充</el-button>
        </div>
        <Table
          @sortChange="sortChange"
          :tableHeader="tableHeader2"
          :tableData="tableData2"
          selection
          class="mt10"
          :isLoading="isLoading2"
          @confirmChange="confirmChange2"
          @selectList="selectTableList2"
          @currentRow="currentRow2"
        />
        <PageModule
          :PageData="{ total: total2, pageSizeShow: true, buttonS: true }"
          :layout="'prev, pager'"
          @pageSizeChange="handleSizeChange2"
          @pageChange="handleCurrentChange2"
        />
      </el-card>
    </div>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import LoginWork from '@/utils/LoginWork'
import { selectList } from '@/plugins/mixin'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'InstructionCostOperationKl'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { onMounted, watch } from 'vue'
const labour = ref('')
const charge = ref('')
const formList = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '品牌',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'date',
    dateLabel: '下单日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  }
])

const formList2 = ref<any>([
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '型体',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '品牌',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '出货日期',
    dateValue: ''
  },
  {
    type: 'date',
    dateLabel: '客人交期',
    dateValue: ''
  },
  {
    type: 'checkbox',
    checkboxLabel: '已封账',
    checked: true
  },
  {
    type: 'checkbox',
    checkboxLabel: '未封账',
    checked: true
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '插入计算'
  },
  {
    type: 'button',
    checkboxLabel: '封账批处理'
  },
  {
    type: 'button',
    checkboxLabel: '取消封账'
  },
  {
    type: 'button',
    checkboxLabel: '完工删除'
  }
])

const tableHeader = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '工厂型体',
    prop: 'style'
  },
  {
    label: '品牌',
    prop: 'custom'
  },
  {
    label: '订单数',
    prop: 'orderQty'
  },
  {
    label: '预补数',
    prop: 'surplusQty'
  },
  {
    label: '下单日期',
    prop: 'orderDate'
  },
  {
    label: '客人交期',
    prop: 'deliveryDate'
  },
  {
    label: '产出次数',
    prop: 'lastQs'
  }
])
// 表一模块
// 表一按钮
const findClicked = (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}
const tableData = ref([])
const imgList = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
// 表格计算函数
const cfmFlag = () => {
  const Unaudited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '有欠数')
  const Audited = formList.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '无欠数')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return ''
  } else if (Unaudited.checked) {
    return 'N'
  } else if (Audited.checked) {
    return 'Y'
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return ''
  }
}
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>('/pcLotCost/getNotCompleteLotDetail', {
    custom: formList.value.find((item: any) => item.selectP && Jzhcn.Find[item.selectP.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌')
      ?.selectValue,
    deliveryDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[1],
    deliveryDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[0],
    flag: '',
    lotNo: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    orderDateEnd: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[1],
    orderDateStart: formList.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '下单日期'
    )?.dateValue?.[0],
    outDateEnd: '',
    outDateStart: '',
    sort: '',
    style: formList.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
    )?.inputValue,
    pageNum: currentPage.value,
    pageSize: pageSize.value
  })
  isLoading.value = false
  tableData.value = tableDataList.list
  total.value = tableDataList.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0] ? tableData.value[0] : {}) => {
  currentTableRow.value = row
}

const imgList2 = ref([])
const tableHeader2 = ref([
  {
    change: true,
    label: '品牌',
    prop: 'custom'
  },
  {
    sortable: 'custom',
    change: true,
    backgroundColor: true,
    label: '指令',
    prop: 'lotNo'
  },
  {
    backgroundColor: true,
    change: true,
    label: '型体',
    prop: 'style'
  },
  {
    change: true,
    label: '颜色',
    prop: 'color'
  },
  {
    change: true,
    label: '订单数',
    prop: 'orderQty'
  },
  {
    change: true,
    label: '出货欠数',
    prop: 'outQtyQs'
  },
  {
    change: true,
    label: '下单日期',
    prop: 'orderDate'
  },
  {
    change: true,
    label: '指令交期',
    prop: 'jQDate'
  },
  {
    change: true,
    label: '出货日期',
    prop: 'outDate'
  },
  {
    label: '成型人工',
    prop: 'lastFee'
  },
  {
    label: '管理费用',
    prop: 'adminFee'
  },
  {
    change: true,
    label: '指令成本',
    prop: 'amt'
  },
  {
    change: true,
    label: '操作标识',
    prop: 'remark'
  },
  {
    change: true,
    label: '操作员',
    prop: 'sysUser'
  },
  {
    change: true,
    label: '封账标识',
    prop: 'cfmFlag'
  },
  {
    label: '财务备注',
    prop: 'remarkCw'
  },
  {
    type: 'img',
    label: '图片',
    fixed: true,
    prop: 'photoPath',
    srcList: imgList2
  }
])
// 表二模块
// 表二按钮
const findClicked2 = async (content: string) => {
  if (content === '置空') {
    formList2.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '插入计算') {
    if (selectedLists.value.length === 0) {
      JMessage.warning('请勾选待完工指令数据')
      return
    }
    const res = (await LoginWork.post(`/pcLotCost/addCostBatch?flag=${1}`, selectedLists.value)) as any
    if (res.code === 200) {
      JMessage.success('插入计算成功')
      getTableList()
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '封账批处理') {
    if (selectedLists2.value.length === 0) {
      JMessage.warning('请勾选已完工指令数据')
      return
    }
    if (selectedLists2.value.some((item: any) => item.cfmFlag === 'Y')) {
      JMessage.warning('封账批处理失败，请勿重复封账')
      return
    }
    const res = (await LoginWork.post(
      `/pcLotCost/sealAccount`,
      selectedLists2.value.map((item: any) => item.lotNo)
    )) as any
    if (res.code === 200) {
      JMessage.success('封账批处理成功')
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '取消封账') {
    if (selectedLists2.value.length === 0) {
      JMessage.warning('请勾选已完工指令数据')
      return
    }
    const res = (await LoginWork.post(
      `/pcLotCost/cancelSealAccount`,
      selectedLists2.value.map((item: any) => item.lotNo)
    )) as any
    if (res.code === 200) {
      JMessage.success('取消封账成功')
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
  if (content === '完工删除') {
    if (selectedLists2.value.length === 0) {
      JMessage.warning('请勾选已完工指令数据')
      return
    }
    if (selectedLists2.value.some((item: any) => item.cfmFlag === 'Y')) {
      JMessage.warning('完工删除失败，有数据已封账')
      return
    }
    const res = (await LoginWork.post(
      `/pcLotCost/deleteCostBatch`,
      selectedLists2.value.map((item: any) => item.lotNo)
    )) as any
    if (res.code === 200) {
      JMessage.success('完工删除成功')
      getTableList()
      getTableList2()
    } else {
      JMessage.error(res.message)
    }
  }
}
const selectedLists = ref([])
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
const tableData2 = ref([])
const isLoading2 = ref(true)
// 分页器
const currentPage2 = ref(1)
const pageSize2 = ref(40)
const total2 = ref(0)
const handleSizeChange2 = (val: number) => {
  pageSize2.value = val
  getTableList2()
}
const handleCurrentChange2 = (val: number) => {
  currentPage2.value = val
  getTableList2()
}
// 表格计算函数
const cfmFlag2 = () => {
  const Unaudited = formList2.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '已封账')
  const Audited = formList2.value.find((item: any) => item.type === 'checkbox' && i18n.global.t(item.checkboxLabel) === '未封账')
  if (!Unaudited || !Audited) return
  if (Unaudited.checked && Audited.checked) {
    return 3
  } else if (Unaudited.checked) {
    return 1
  } else if (Audited.checked) {
    return 2
  } else {
    ElMessage('必须至少选择一项')
    Unaudited.checked = true
    Audited.checked = true
    return 3
  }
}
const order = ref(3)
const sortChange = (sort: any, prop: any, column: any) => {
  if (sort === 'ascending') {
    console.log('升序')
    order.value = 1
  } else if (sort === 'descending') {
    console.log('降序')
    order.value = 2
  } else if (sort === null) {
    order.value = 3
  }
  getTableList2()
}
// 获取表数据
const getTableList2 = debounce(async () => {
  isLoading2.value = true
  let tableDataList = await service.post<any, any>('/pcLotCost/getCompleteLotDetail', {
    custom: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '品牌'
    )?.inputValue,
    deliveryDateEnd: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[1],
    deliveryDateStart: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '客人交期'
    )?.dateValue?.[0],
    flag: cfmFlag2(),
    lotNo: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '指令'
    )?.inputValue,
    orderDateEnd: '',
    orderDateStart: '',
    outDateEnd: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
    )?.dateValue?.[1],
    outDateStart: formList2.value.find(
      (item: any) => item.dateLabel && Jzhcn.Find[item.dateLabel.split('.')[1] as keyof typeof Jzhcn.Find] === '出货日期'
    )?.dateValue?.[0],
    sort: order.value,
    style: formList2.value.find(
      (item: any) => item.inputPlaceholder && Jzhcn.Find[item.inputPlaceholder.split('.')[1] as keyof typeof Jzhcn.Find] === '型体'
    )?.inputValue,
    pageNum: currentPage2.value,
    pageSize: pageSize2.value
  })
  isLoading2.value = false
  imgList2.value = tableDataList.list.map((item: any) => {
    return item.photoPath
  })
  tableData2.value = tableDataList.list.map((item: any) => {
    if (item.cfmFlag === 'Y') item.backgroundColor = '#80FF00'
    if (item.orderQty) item.orderQty = Number(item.orderQty).toFixed(2)
    if (item.amt) item.amt = Number(item.amt).toFixed(2)
    item.change = 'Y'
    return item
  })
  total2.value = tableDataList.total
  // currentRow2()
}, 500)
onMounted(async () => {
  getTableList2()
  await selectList(formList, () => service.get('/back/order/allOrderCondition'))
})
watch(
  () =>
    formList2.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList2()
  }
)
// 点击表给另一张表传数据
const currentTableRow2 = ref({}) as any
const currentRow2 = async (row: any = tableData.value[0]) => {
  currentTableRow2.value = row
}
const selectedLists2 = ref([])
const selectTableList2 = (selectedList: any) => {
  selectedLists2.value = selectedList
}

const tableHeader3 = ref([
  {
    label: '材料名称',
    prop: 'slipNo'
  },
  {
    label: '材料颜色',
    prop: 'slipDate'
  },
  {
    label: '规格',
    prop: 'dutyDeptName'
  },
  {
    label: '单位',
    prop: 'sysUser'
  },
  {
    label: '加工方式',
    prop: 'slipNo'
  },
  {
    label: '比例',
    prop: 'slipDate'
  },
  {
    label: '是否请购',
    type: 'checkbox',
    prop: 'checkboxArrange'
  }
])
const addList = async () => {
  if (selectedLists2.value.length === 0) {
    JMessage.warning('请选择完工指令')
    return
  }
  if (selectedLists2.value.some((item: any) => item.cfmFlag === 'Y')) {
    JMessage.warning('有已封账指令，请先取消封账')
    return
  }
  const res = (await LoginWork.post('/pcLotCost/updateCostBatch', {
    adminFee: charge.value,
    lastFee: labour.value,
    lotNoList: selectedLists2.value.map((item: any) => item.lotNo)
  })) as any
  if (res.code === 200) {
    JMessage.success('填充成功')
    getTableList2()
  } else {
    JMessage.error(res.message)
  }
}
const confirmChange2 = async (row: any) => {
  const res = (await LoginWork.post('/pcLotCost/updateCost', {
    adminFee: row.adminFee,
    lastFee: row.lastFee,
    remarkCw: row.remarkCw,
    lotNo: row.lotNo
  })) as any
  if (res.code === 200) {
    JMessage.success('修改成功')
    getTableList2()
  } else {
    JMessage.error(res.message)
  }
}
</script>

<style lang="less" scoped></style>
